# -*- coding: utf-8 -*-
import random
import os
import util
import time
import mp3play

class Side(object):
    def activate(self): pass
    def cycle(self, backwards=False): return False
    def render(self, painter): pass

class ImageSide(Side):
    def __init__(self, images):
        self.images = images
        self.index = 0
        
    def cycle(self, backwards=False):
        if len(self.images) <= 1: return False
        self.index += -1 if backwards else 1
        self.index %= len(self.images)
        return True
        
    def render(self, painter):
        painter.draw_image(self.images[self.index].image)

class TextSide(Side):
    def __init__(self, text):
        self.text = text
        
    def render(self, painter):
        painter.draw_text(self.text.text)

class AudioSide(Side):
    def __init__(self, text, sounds=None):
        self.text = text
        self.sounds = sounds
        
    def activate(self):
        if self.sounds:
            util.play_sound(self.sounds[0].sound)
    
    def render(self, painter):
        painter.draw_text(self.text.text)

class Card(object):
    def __init__(self, concept):
        self.concept = concept
        self.sides = []
        self.index = 0
        
        if self.concept.images:
            self.sides.append(ImageSide(self.concept.images))
           
        if self.concept.texts:
            self.sides.append(TextSide(self.concept.texts[0]))
            
        if self.concept.phonetic_text:
            self.sides.append(AudioSide(self.concept.phonetic_text, self.concept.sounds))
        
        
    def flip(self, backwards=False):
        self.index += -1 if backwards else 1
        self.index %= len(self.sides)
        self.sides[self.index].activate()
    
    def cycle(self, backwards=False):
        return self.sides[self.index].cycle(backwards)
    
    def random_side(self):
        self.index = random.randint(0, len(self.sides) - 1)
    
    def activate(self):
        self.sides[self.index].activate()
    
    def render(self, painter):
        self.sides[self.index].render(painter)
        
if __name__ == "__main__":
    f = r"c:\users\jacobsm\appdata\local\temp\tmpjempe0.mp3"
    #pyglet.app.run()
    #pyglet.media.load(f).play()
    
    mp3play.load(r"c:\users\jacobsm\appdata\local\temp\tmpxzpopq.mp3").play()
    time.sleep(3)
    
    #e.exit()
    
    
    